﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Xml;
using System.Reflection;

namespace Bornander.UI
{
    static public class XMLutil
    {
        //private static string filepath = @"\Storage Card\XMLDB";
        private static string filepath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

        private static XmlReader reader;
        //private static enum Eatery { };
        private static List<Eatery> eateryList;

        #region 找食
        #region 找食店数据加载
        /// <summary>
        /// 加载数据
        /// </summary>
        private static void InitFindFood()
        {
            reader = XmlReader.Create(filepath + @"\FoodInfo.xml");
            eateryList = new List<Eatery>();
            while (reader.ReadToFollowing("Eatery"))
            {
                Eatery tmpEat = new Eatery();
                reader.ReadToFollowing("Name");
                tmpEat.Name = reader.ReadElementContentAsString();

                reader.ReadToFollowing("Cost");
                tmpEat.Cost = reader.ReadElementContentAsInt();

                reader.ReadToFollowing("Place");
                tmpEat.Place = reader.ReadElementContentAsString();

                reader.ReadToFollowing("Address");
                tmpEat.Address = reader.ReadElementContentAsString();

                reader.ReadToFollowing("Phone");
                tmpEat.Phone = reader.ReadElementContentAsString();

                reader.ReadToFollowing("Image");
                Bitmap bmp = new Bitmap(filepath + "\\" + reader.ReadElementContentAsString());
                tmpEat.Image = bmp;

                reader.ReadToFollowing("Description");
                tmpEat.Description = reader.ReadElementContentAsString();

                reader.ReadToFollowing("Taste");
                tmpEat.Taste = reader.ReadElementContentAsInt();

                reader.ReadToFollowing("Envir");
                tmpEat.Envir = reader.ReadElementContentAsInt();

                reader.ReadToFollowing("Server");
                tmpEat.Server = reader.ReadElementContentAsInt();

                reader.ReadToFollowing("Price");
                tmpEat.Price = reader.ReadElementContentAsInt();

                reader.ReadToFollowing("Again");
                tmpEat.Again = reader.ReadElementContentAsInt();

                reader.ReadToFollowing("MarkCount");
                tmpEat.MarkCount = reader.ReadElementContentAsInt();

                eateryList.Add(tmpEat);
            }
            reader.Close();
        }
        #endregion
        #region 找食店methods
        private static List<Eatery> FindStyle(string style, List<Eatery> findlist)
        {
            if (style == "" || style == null)
            {
                return findlist;
            }
            else
            {
                List<Eatery> tmp = new List<Eatery>();
                foreach (Eatery tmpeat in findlist)
                {
                    if (tmpeat.Style == style)
                    {
                        tmp.Add(tmpeat);
                    }
                }
                return tmp;
            }
            //return new List<Eatery>();
        }

        private static List<Eatery> FindCost(int cost, List<Eatery> findlist)
        {
            if (cost==0)
            {
                return findlist;
            }
            else
            {
                List<Eatery> tmp = new List<Eatery>();
                foreach (Eatery tmpeat in findlist)
                {
                    if (tmpeat.Cost <= cost)
                    {
                        tmp.Add(tmpeat);
                    }
                }
                return tmp;
            }
            //return new List<Eatery>();
        }

        private static List<Eatery> FindPlace(string place, List<Eatery> findlist)
        {
            if (place == "" || place == null)
            {
                return findlist;
            }
            else
            {
                List<Eatery> tmp = new List<Eatery>();
                foreach (Eatery tmpeat in findlist)
                {
                    if (tmpeat.Place == place)
                    {
                        tmp.Add(tmpeat);
                    }
                }
                return tmp;
            }
            //return new List<Eatery>();
        }

        public static List<Eatery> FindFood(string style, int cost, string place)
        {
            InitFindFood();
            return FindPlace(place, FindCost(cost, FindStyle(style, eateryList)));
        }
        #endregion
        #endregion

        #region 食店评论
        #region 读取评论
        public static List<EateryComment> GetComments(string name)
        {
            reader = XmlReader.Create(filepath + @"\FoodComment.xml");
            List<EateryComment> commentList = new List<EateryComment>();
            while (reader.ReadToFollowing("Commnet"))
            {
                EateryComment tmpEat = new EateryComment();
                reader.ReadToFollowing("Name");
                tmpEat.Name = reader.ReadElementContentAsString();
                if (tmpEat.Name != name)
                {
                    continue;
                }

                reader.ReadToFollowing("Reviewer");
                tmpEat.Reviewer = reader.ReadElementContentAsString();

                reader.ReadToFollowing("Taste");
                tmpEat.Taste = reader.ReadElementContentAsInt();

                reader.ReadToFollowing("Envir");
                tmpEat.Envir = reader.ReadElementContentAsInt();

                reader.ReadToFollowing("Service");
                tmpEat.Service = reader.ReadElementContentAsInt();

                reader.ReadToFollowing("Price");
                tmpEat.Price = reader.ReadElementContentAsInt();

                reader.ReadToFollowing("Again");
                tmpEat.Again = reader.ReadElementContentAsBoolean();

                reader.ReadToFollowing("Remark");
                tmpEat.Remark = reader.ReadElementContentAsString();

                commentList.Add(tmpEat);
            }
            reader.Close();

            return new List<EateryComment>();
        }
        #endregion

        #region 添加评论
        public static void AddComment( Eatery eatery, string name, string reviewer, int taste, int envir, int service, int price, bool again, string remark)
        {
            reader = XmlReader.Create(filepath + @"\FoodComment.xml");
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(reader);

            XmlElement xele = xdoc.CreateElement("Comment");

            XmlElement xname = xdoc.CreateElement("Name");
            xname.InnerText = name;
            xele.AppendChild(xname);

            XmlElement xreviewer = xdoc.CreateElement("Reviewer");
            xreviewer.InnerText = reviewer;
            xele.AppendChild(xreviewer);

            XmlElement xtaste = xdoc.CreateElement("Taste");
            xtaste.InnerText = taste.ToString();
            xele.AppendChild(xtaste);

            XmlElement xenvir = xdoc.CreateElement("Envir");
            xenvir.InnerText = envir.ToString();
            xele.AppendChild(xenvir);

            XmlElement xservice = xdoc.CreateElement("Service");
            xservice.InnerText = service.ToString();
            xele.AppendChild(xservice);

            XmlElement xprice = xdoc.CreateElement("Price");
            xprice.InnerText = price.ToString();
            xele.AppendChild(xprice);

            XmlElement xagain = xdoc.CreateElement("Again");
            xagain.InnerText = again.ToString();
            xele.AppendChild(xagain);

            XmlElement xremark = xdoc.CreateElement("Remark");
            xremark.InnerText = remark;
            xele.AppendChild(xremark);

            xdoc.DocumentElement.AppendChild(xele);

            reader.Close();

            xdoc.Save(filepath + @"\FoodComment.xml");

            eatery.Taste += taste;
            eatery.Envir += envir;
            eatery.Server += service;
            eatery.Price += price;
            eatery.Again += again ? 1 : 0;
            eatery.MarkCount++;

            EateryMarkUpdate(eatery);
        }
        #endregion

        #region 更新食店评分
        private static void EateryMarkUpdate(Eatery eatery)
        {
            reader = XmlReader.Create(filepath + @"\FoodInfo.xml");
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(reader);

            XmlNodeList xnl = xdoc.GetElementsByTagName("Eatery");
            for (int i = 0; i < xnl.Count; i++)
            {
                if (xnl[i].ChildNodes[0].Value == eatery.Name)
                {
                    xnl[i].ChildNodes[8].ChildNodes[0].Value = eatery.Taste.ToString();
                    xnl[i].ChildNodes[8].ChildNodes[1].Value = eatery.Envir.ToString();
                    xnl[i].ChildNodes[8].ChildNodes[2].Value = eatery.Server.ToString();
                    xnl[i].ChildNodes[8].ChildNodes[3].Value = eatery.Price.ToString();

                    reader.Close();

                    xdoc.Save(filepath + @"\FoodInfo.xml");
                    return;
                }
            }

            reader.Close();

            xdoc.Save(filepath + @"\FoodInfo.xml");
        }
        #endregion
        #endregion

        #region 用户注册与登陆
        #region 用户登录
        public static UserInfo Login(string email, string pwd)
        {
            reader = XmlReader.Create(filepath + @"\UserInfo.xml");

            while (reader.ReadToFollowing("User"))
            {
                UserInfo tmpuser = new UserInfo();
                reader.ReadToFollowing("Email");
                tmpuser.Email = reader.ReadElementContentAsString();
                if (tmpuser.Email == email)
                {
                    reader.ReadToFollowing("Password");
                    tmpuser.Password = reader.ReadElementContentAsString();

                    if (tmpuser.Password == pwd)
                    {
                        reader.ReadToFollowing("Nick");
                        tmpuser.Nick = reader.ReadElementContentAsString();

                        reader.ReadToFollowing("University");
                        tmpuser.University = reader.ReadElementContentAsString();

                        reader.ReadToFollowing("Sex");
                        tmpuser.Sex = reader.ReadElementContentAsString();

                        reader.Close();
                        return tmpuser;
                    }
                    else
                    {
                        reader.Close();
                        return null;
                    }
                }
                else
                {
                    continue;
                }
            }

            reader.Close();
            return null;
        }
        #endregion

        #region 用户注册
        public static UserInfo Register(string email, string password, string nick, string university, string sex)
        {
            UserInfo user = new UserInfo();
            user.Email = email;
            user.Password = password;
            user.Nick = nick;
            user.University = university;
            user.Sex = sex;

            reader = XmlReader.Create(filepath + @"\UserInfo.xml");
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(reader);

            XmlElement xele = xdoc.CreateElement("User");

            XmlElement xemail = xdoc.CreateElement("Email");
            xemail.InnerText = email;
            xele.AppendChild(xemail);

            XmlElement xpwd = xdoc.CreateElement("Password");
            xpwd.InnerText = password;
            xele.AppendChild(xpwd);

            XmlElement xnick = xdoc.CreateElement("Nick");
            xnick.InnerText = nick;
            xele.AppendChild(xnick);

            XmlElement xuniv = xdoc.CreateElement("University");
            xuniv.InnerText = university;
            xele.AppendChild(xuniv);

            XmlElement xsex = xdoc.CreateElement("Sex");
            xsex.InnerText = sex;
            xele.AppendChild(xsex);

            xdoc.DocumentElement.AppendChild(xele);

            reader.Close();

            xdoc.Save(filepath + @"\UserInfo.xml");

            return user;
        }
        #endregion
        #endregion

        #region 活动
        #region 找指定类型活动
        /// <summary>
        /// 获取指定类型活动列表
        /// <para type>活动类型</para>
        /// </summary>
        public static List<Activity> GetActivityList(string type)
        {
            reader = XmlReader.Create(filepath + @"\Activity.xml");
            List<Activity> actList = new List<Activity>();
            while (reader.ReadToFollowing("Activity"))
            {
                reader.ReadToFollowing("Type");
                if (reader.ReadElementContentAsString() != type )
                {
                    continue;
                }

                Activity tmpAct = new Activity();

                reader.ReadToFollowing("Title");
                tmpAct.Title = reader.ReadElementContentAsString();

                reader.ReadToFollowing("Organiger");
                tmpAct.Organiger = reader.ReadElementContentAsString();

                reader.ReadToFollowing("Shop");
                tmpAct.Shop = reader.ReadElementContentAsString();

                reader.ReadToFollowing("Place");
                tmpAct.Place = reader.ReadElementContentAsString();

                reader.ReadToFollowing("StartTime");
                tmpAct.StartTime = reader.ReadElementContentAsString();

                reader.ReadToFollowing("EndTime");
                tmpAct.EndTime = reader.ReadElementContentAsString();

                reader.ReadToFollowing("AttendeeLimit");
                tmpAct.AttendeeLimit = reader.ReadElementContentAsInt();

                reader.ReadToFollowing("AttendeeNum");
                tmpAct.AttendeeNum = reader.ReadElementContentAsInt();

                reader.ReadToFollowing("Description");
                tmpAct.Description = reader.ReadElementContentAsString();


                actList.Add(tmpAct);
            }
            reader.Close();

            return actList;
        }
        #endregion

        #region 发起活动
        public static Activity AddActivity(string type, string title, string organiger, string shop, string place, string starttime, string endtime, int attendeelimit, int attendeenum, string description)
        {
            reader = XmlReader.Create(filepath + @"\Activity.xml");
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(reader);

            XmlElement xele = xdoc.CreateElement("Activity");

            XmlElement xtype = xdoc.CreateElement("Type");
            xtype.InnerText = type;
            xele.AppendChild(xtype);

            XmlElement xtitle = xdoc.CreateElement("Title");
            xtitle.InnerText = title;
            xele.AppendChild(xtitle);

            XmlElement xorganiger = xdoc.CreateElement("Organiger");
            xorganiger.InnerText = organiger;
            xele.AppendChild(xorganiger);

            XmlElement xshop = xdoc.CreateElement("Shop");
            xshop.InnerText = shop;
            xele.AppendChild(xshop);

            XmlElement xplace = xdoc.CreateElement("Place");
            xplace.InnerText = place;
            xele.AppendChild(xplace);

            XmlElement xst = xdoc.CreateElement("StartTime");
            xst.InnerText = starttime;
            xele.AppendChild(xst);

            XmlElement xet = xdoc.CreateElement("EndTTime");
            xet.InnerText = endtime;
            xele.AppendChild(xet);

            XmlElement xal = xdoc.CreateElement("AttendeeLimit");
            xal.InnerText = attendeelimit.ToString();
            xele.AppendChild(xal);

            XmlElement xan = xdoc.CreateElement("AttendeeNum");
            xan.InnerText = attendeenum.ToString();
            xele.AppendChild(xan);

            XmlElement xdesc = xdoc.CreateElement("Description");
            xdesc.InnerText = description;
            xele.AppendChild(xdesc);

            xdoc.DocumentElement.AppendChild(xele);

            reader.Close();

            xdoc.Save(filepath + @"\Activity.xml");

            Activity act = new Activity();
            act.Title = title;
            act.Shop = shop;
            act.Organiger = organiger;
            act.Place = place;
            act.StartTime = starttime;
            act.EndTime = endtime;
            act.AttendeeLimit = attendeelimit;
            act.AttendeeNum = attendeenum;
            act.Description = description;

            return act;
        }
        #endregion
        #endregion
    }
}